package com.caimore;

import com.caimore.common.Serial_For_Smslib;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SMSRecvMsg;
import com.caimore.entity.SendSmsParam;
import com.caimore.logger.Logger;
import com.caimore.module.ec200n.ATReaderThreadPduEC200N;
import com.caimore.module.ec200n.ATWriterThreadPduEC200N;
import com.caimore.module.m35.ATReaderThreadPduM35;
import com.caimore.module.m35.ATWriterThreadPduM35;
import com.caimore.module.mc8332.ATReaderThreadPDU8332;
import com.caimore.module.mc8332.ATWriterThreadPDU8332;
import com.caimore.module.mu509.ATReaderThreadPduMU509;
import com.caimore.modulefactory.IsmsReader;
import com.caimore.modulefactory.IsmsWriter;
import com.caimore.modulefactory.SmsFactory;
import java.util.ArrayList;

public class FFSMSSdk {

	Serial_For_Smslib SerialSmslib = null;
	String comName = "COM10";
	int baudrate = 115200;
	boolean RsBool = false;
	volatile boolean FlagBool = false;

	public FFSMSSdk(String sComName, int iBaudrate) {
		this.comName = sComName;
		this.baudrate = iBaudrate;
		System.out.println("[FFSMSSdk]init");
	}

	public boolean startSMSServer() {
		boolean StartBool = false;

		try {
			if (this.SerialSmslib != null) {
				this.SerialSmslib.closePort();
				this.SerialSmslib = null;
			}

			this.SerialSmslib = new Serial_For_Smslib();
			this.RsBool = this.SerialSmslib.openPort(this.comName, this.baudrate);
			System.out.println("openPort:" + this.RsBool);
			if (this.RsBool) {
				SmsFactory.getInstance().pushWriterInstance("M35", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("M35", new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("MC37i",
						new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("MC37i",
						new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("GL868",
						new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("GL868",
						new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("GL865",
						new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("GL865",
						new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("MC8332",
						new ATWriterThreadPDU8332(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("MC8332",
						new ATReaderThreadPDU8332(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("MU509",
						new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("MU509",
						new ATReaderThreadPduMU509(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("MU709",
						new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("MU709", new ATReaderThreadPduMU509(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("U7500", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("U7500", new ATReaderThreadPduMU509(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("MU609", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("MU609", new ATReaderThreadPduMU509(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("SIM7600CE", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("SIM7600CE", new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("N720", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("N720", new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("EC20F", new ATWriterThreadPduM35(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("EC20F", new ATReaderThreadPduM35(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("EC200N", new ATWriterThreadPduEC200N(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("EC200N", new ATReaderThreadPduEC200N(this.SerialSmslib, this.comName, false));
				SmsFactory.getInstance().pushWriterInstance("EC200S", new ATWriterThreadPduEC200N(this.SerialSmslib, false));
				SmsFactory.getInstance().pushReaderInstance("EC200S", new ATReaderThreadPduEC200N(this.SerialSmslib, this.comName, false));

				String ModuleType;
				int e;
				for (e = 0; e < 3; ++e) {
					ModuleType = "AT+GMM\r";
					char[] oIsmsWriter = ModuleType.toCharArray();
					this.SerialSmslib.writeByte(oIsmsWriter);
					System.out.println("[ATWriterThreadSmsType]sSend-AT-Command:" + ModuleType);
					Thread.sleep(100L);
				}

				e = 0;
				ModuleType = SMSMessageMgr.getInstance().getSmsType();
				while (ModuleType == null) {
					ModuleType = SMSMessageMgr.getInstance().getSmsType();
					++e;
					if (ModuleType != null || e == 10) {
						break;
					}
				}

				if (ModuleType != null) {
					System.out.println("[FFSMSSdk]startSMSServer ,sSmsType: " + ModuleType);
					IsmsWriter writer = SmsFactory.getInstance().popWriterInstance(ModuleType);
					if (writer != null) {
						writer.start();
					} else {
						Logger.error("[FFSMSSdk]Can not find the device type");
					}

					IsmsReader oIsmsReader = SmsFactory.getInstance().popReaderInstance(ModuleType);
					if (oIsmsReader != null) {
						oIsmsReader.start();
					}

					StartBool = true;
				}

				System.out.println("[FFSMSSdk]startSMSServer:open success");
			} else {
				SMSMessageMgr.getInstance().setERROR(1);
				System.out.println("[FFSMSSdk]startSMSServer:open error");
			}

			Logger.init("INFO", 3);
		} catch (Exception var6) {
			System.out.println(var6.getMessage());
		}

		return StartBool;
	}

	public void stopSMSServer() {
		try {
			String e = SMSMessageMgr.getInstance().getSmsType();
			if (e != null) {
				System.out.println("[FFSMSSdk] stopSMSServer stop ,sSmsType:" + e);
				IsmsWriter oIsmsWriter = SmsFactory.getInstance().popWriterInstance(e);
				if (oIsmsWriter != null) {
					oIsmsWriter.shutdownRequest();
					oIsmsWriter = null;
				}

				IsmsReader oIsmsReader = SmsFactory.getInstance().popReaderInstance(e);
				if (oIsmsReader != null) {
					oIsmsReader.shutdownRequest();
					oIsmsReader = null;
				}
			}

			if (this.SerialSmslib != null) {
				this.SerialSmslib.closePort();
				this.SerialSmslib = null;
			}
		} catch (Exception var4) {
			System.out.println("[FFSMSSdk]stopSMSServer " + var4.getMessage());
		}

	}

	public boolean sendSMSMsg(String sMsg, String sPhone) {
		if (sPhone != null) {
			String[] phones = sPhone.split(",");
			for (String sTelPhone : phones) {
				if (sMsg != null && sTelPhone != null && sMsg.trim().length() > 0 && sTelPhone.trim().length() > 0) {
					SendSmsParam oSendSmsParam = new SendSmsParam();
					oSendSmsParam.setContent(sMsg);
					oSendSmsParam.setPhone(sTelPhone);
					SMSMessageMgr.getInstance().addSendSmsParam(oSendSmsParam);
				}
			}
		}

		Thread var9 = new Thread() {
			public void run() {
				try {
					Thread.sleep(6000L);
				} catch (InterruptedException var2) {
					var2.printStackTrace();
				}

				if (SMSMessageMgr.getInstance().getOK()) {
					FFSMSSdk.this.FlagBool = true;
					Logger.info("[FFSMSSdk.sendSMSMsg]: true");
				} else {
					FFSMSSdk.this.FlagBool = false;
					Logger.info("[FFSMSSdk.sendSMSMsg]: flase");
				}

			}
		};
		var9.start();

		while (!SMSMessageMgr.getInstance().getOK()) {
			if (SMSMessageMgr.getInstance().getOK()) {
				this.FlagBool = true;
				break;
			}

			SMSMessageMgr.getInstance().getOK();
		}

		return this.FlagBool;
	}

	public ArrayList<SMSRecvMsg> recvSMSMsg() {
		return SMSMessageMgr.getInstance().delRecvCMGLMessage();
	}

	public int getLastError() {
		return SMSMessageMgr.getInstance().getERROR();
	}

	public boolean getIsSend() {
		return this.FlagBool;
	}
}
